//logs.js
const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象

Page({
  data: {
    titleArr:[],
    isSelect:false,
    icdels:[],
    prell:[],
    imgarr:[],
    submin:true,
    xiao:[{
      name:'超级满意',
      iconpath:'../../../images/supergood.jpg',
      iconpath_ed:'../../../images/seloij_03.jpg',
      maxnum:5,
      status:false
    },{
        name: '满意',
        iconpath: '../../../images/verygood.jpg',
        iconpath_ed: '../../../images/seloij_05.jpg',
        maxnum: 4,
        status: false
      },
      {
        name: '还可以',
        iconpath: '../../../images/good.jpg',
        iconpath_ed: '../../../images/seloij_07.jpg',
        maxnum: 3,
        status: false
      },
      {
        name: '不满意',
        iconpath: '../../../images/nogood.jpg',
        iconpath_ed: '../../../images/seloij_09.jpg',
        maxnum: 2,
        status: false
      },{
        name: '很失望',
        iconpath: '../../../images/verybad.jpg',
        iconpath_ed: '../../../images/seloij_11.png',
        maxnum: 1,
        status: false
      }],
      id:'',
      desc:'',
      star:1,
    icdels:[],
    activeindex:0
  },
  onLoad: function (options) {
    this.setData({id: options.id})
    this._getdiscuss(1)
  },
  setPlain(e){  //提交计划
    let that=this;
    let form_id = e.detail.formId;
    that.setData({
      form_id: form_id
    })
    this._getdiscuss(2)
  },
  selected(e){
    var index = parseInt(e.currentTarget.dataset.index);
    if(index == this.data.activeindex){return;}
    this.setData({
      activeindex:index
    })
  },
  // 多选
  selectApply: function (e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.icdels[index];
    item.isSelect = !item.isSelect;
    this.setData({
      icdels: this.data.icdels,
      activeindex: index
    });
    
    if (this.data.icdels[this.data.activeindex].isSelect == true) {
      let titleArr = this.data.titleArr;
      titleArr.push(this.data.icdels[this.data.activeindex].title);
      let newArr = titleArr;
      newArr = newArr.join(',')
      this.setData({
        newArr: newArr
      })
    }
  },

  _getdiscuss(type){
    let that =this;
    if(type == 1){
      index.getdiscuss({},(res)=>{
        if(res.code == 200){
          that.setData({
            icdels:res.data
          })
        }
      })
    }else{
      that.setData({
        submin:false
      })
      let data ={
        id:that.data.id,
        title: that.data.newArr,
        desc:that.data.desc,
        star:that.data.star,
        pic:that.data.imgarr.join(','),
        form_id: that.data.form_id
      }
      index.getdiscuss(data,(res)=>{
        if(res.code == 200){
          wx.showToast({
            title: res.msg,
            icon:"none",
            duration:1000
          })
          setTimeout(()=>{
            wx.navigateBack()
          },1100)

        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          })}
      })
    }
  },

  show_iconx(e){
    let index = parseInt(e.currentTarget.dataset.index);
    let xiao = this.data.xiao;
     xiao.forEach((item,i)=>{
        item.status = false;
       if(i == index){
         item.status = true
       }
    })
    let star =  xiao.filter((item,index)=>item.status?item.maxnum:'');
    this.setData({
      xiao,
      star: star[0].maxnum
    })
  },
  getEvarv(e){
    this.setData({
      desc:e.detail.value
    })
  },
  // _getdiscuss(type){
  //   let that =this;
  //   if(type == 1){
  //     index.getdiscuss({},(res)=>{
  //       if(res.code == 200){
  //         that.setData({
  //           icdels:res.data
  //         })
  //       }
  //     })
  //   }else{
  //     let data ={
  //       id:that.data.id,
  //       title: that.data.icdels[that.data.activeindex].title,
  //       desc:that.data.desc,
  //       star:that.data.star,
  //       pic:that.data.imgarr.join(',')
  //     }
  //     index.getdiscuss(data,(res)=>{
  //       if(res.code == 200){
  //         wx.showToast({
  //           title: res.msg,
  //           icon:"none",
  //           duration:1000
  //         })
  //         setTimeout(()=>{
  //           wx.navigateBack()
  //         },1100)
  
  //       } else {
  //         wx.showToast({
  //           title: res.msg,
  //           icon: "none",
  //           duration: 1000
  //         })}
  //     })
  //   }
  // },
  selctPhone(e){
    let that =this;
    wx.chooseImage({
      count: 5,
      success(res) {
        let prell = that.data.prell;
        let tempFilePaths = res.tempFilePaths;
        let prellarr = prell.concat(tempFilePaths);
        that.setData({
          prell: prellarr
        })
        that.Upload(tempFilePaths,function(res){
          let imgarr = that.data.imgarr;
          let imgarr_ed = imgarr.concat(res);
          that.setData({
            imgarr:imgarr_ed
          })
        })
      }
    })
  },
  preimg(e){
    var index = parseInt(e.currentTarget.dataset.index);
    var that = this;
    wx.previewImage({
      urls: that.data.prell,
      current: that.data.prell[index]
    })
  },

  Upload: function(fromarr,fun){
    var len = 0;
    var tolen = fromarr.length;
    var that =this;
    var pusharr = [];
    loadImg(fromarr);
    function loadImg(fromarr_ed){
        wx.uploadFile({
          url: Config.restUrl+'common/fileUp',
          header: {
            "Content-Type": "multipart/form-data",
          },
          filePath: fromarr_ed[len],
          name: 'file',
          formData: {},
          success: function (res) {
              len++;
              var datas = JSON.parse(res.data);
              if (datas.code == 200) { 
                 pusharr.push(datas.data)  
                if (len == tolen){
                  if(fun){
                    fun(pusharr)
                  }
                  return;
                }   
                loadImg(fromarr)      
            }
          },
          fail: function (err) {
          },
          complete: function (res) { },
        })
    } 
  }
})
